//====================================================================================================
// Includes
//====================================================================================================

#include "EnemyIdle.h"

#include "Enemy.h"

//====================================================================================================
// Class Definitions
//====================================================================================================

EnemyIdle::EnemyIdle(Enemy* pOwner)
	: EnemyState(pOwner, "Idle")
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

EnemyIdle::~EnemyIdle()
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

void EnemyIdle::Load(int level)
{
	// fallen, enemy 1
	if ( level == 1 )
		mSprite.Add("enemy01_idle.png");

	// hive, enemy 2
	else if ( level == 2 )
		mSprite.Add("enemy02_idle.png");

	// vex, enemy 3
	else if ( level == 3 )
		mSprite.Add("enemy03_idle.png");

	// cabal, enemy 4
	else if ( level == 4 )
		mSprite.Add("enemy04_idle.png");
}

//----------------------------------------------------------------------------------------------------

void EnemyIdle::Unload()
{
	mSprite.Unload();
}

//----------------------------------------------------------------------------------------------------

void EnemyIdle::Update(float deltaTime)
{
	// Update sprite
	mSprite.Update(deltaTime);

	// State transitions
	mDelay -= deltaTime;

	if (mDelay <= 0.0f)
	{
		mpOwner->ChangeState(eAS_Walk);
		mDelay = RandomFloat(1.0f, 10.0f);
	}

	//mpOwner->ChangeState(eAS_Walk);
}

//----------------------------------------------------------------------------------------------------

void EnemyIdle::Render(const SVector2& offset)
{
	const int kTextureWidth = mSprite.GetWidth();
	const int kTextureHeight = mSprite.GetHeight();
	const SVector2 ownerPos(mpOwner->GetPosition());
	const SVector2 renderPos(ownerPos.x - (kTextureWidth * 0.5f), ownerPos.y - kTextureHeight);
	bool facingLeft = mpOwner->IsFacingLeft();
	mSprite.SetPosition(renderPos - offset);
	mSprite.SetFlipH(facingLeft);
	mSprite.Render();
}

//----------------------------------------------------------------------------------------------------

void EnemyIdle::Enter()
{
	mSprite.Play(4.0f, true);
}

//----------------------------------------------------------------------------------------------------

void EnemyIdle::Exit()
{
	mSprite.Stop();
	mSprite.Reset();
}

//----------------------------------------------------------------------------------------------------

SRect EnemyIdle::GetBoundingBox() const
{
	const SVector2 pos(mpOwner->GetPosition());
	return SRect
	(
		pos.x - 16.0f,
		pos.y - 56.0f,
		pos.x + 16.0f,
		pos.y
	);
}